﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.FormatOutput;
using OrderSystem.NPOIOperateExcel;
using OrderSystem.SystemFramework;
using OrderSystem.Templet;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormQueryOrderItem : FormTemplet
    {

        private int _nCustomerID = -1;

        private int _nProjectID = -1;

        private int _nProductTypeID = -1;

        private DataTable _dtProduct = null;

        public FormQueryOrderItem()
        {
            this.InitializeComponent();
        }

        private void FormQueryCustomer_Load(object sender, EventArgs e)
        {
            this.cbScope.SelectedIndex = 0;
            this.dtpSpeakForDateStart.Enabled = false;
            this.dtpSpeakForDateEnd.Enabled = false;
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            if (this._dtProduct != null)
            {
                this._dtProduct.Dispose();
                this._dtProduct = null;
            }
            if (this.txtProductType.Text.Trim() == "" && this.txtProductCode.Text.Trim() == "" && this.txtWidth.Text.Trim() == "" && this.txtHeight.Text.Trim() == "" && this.txtRemark.Text.Trim() == "" && !this.chkSpeakForDateUsed.Checked)
            {
                MessageBox.Show("请至少填写一个条件", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                OrderItemCondition orderItemCondition = new OrderItemCondition();
                if (this.cbScope.SelectedIndex == 1)
                {
                    orderItemCondition.CustomerID = this._nCustomerID;
                }
                if (this.cbScope.SelectedIndex == 2)
                {
                    orderItemCondition.ProjectID = this._nProjectID;
                }
                if (this.cbScope.SelectedIndex == 3)
                {
                    orderItemCondition.OrderCode = this.txtScope.Text.Trim().Replace("'", "''").Replace(" ", "%");
                }
                if (this.txtProductType.Text.Trim().Length > 0)
                {
                    orderItemCondition.ProductTypeID = this._nProductTypeID;
                }
                if (this.txtProductCode.Text.Trim().Length > 32)
                {
                    MessageBox.Show("产品编号 格式不正确，字符太长，应小于或等于32个字符", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.txtProductCode.Focus();
                }
                else
                {
                    if (this.txtProductCode.Text.Trim().Length > 0)
                    {
                        orderItemCondition.ProductCode = this.txtProductCode.Text.Trim().Replace("'", "''").Replace(" ", "%");
                    }
                    if (this.txtWidth.Text.Trim().Length > 0)
                    {
                        if (!Regex.IsMatch(this.txtWidth.Text.Trim(), "^(([0-9]{1,8})|([0-9]{1,8}\\.[0-9]{1,4}))$") || Convert.ToDouble(this.txtWidth.Text.Trim()) <= 0.0)
                        {
                            MessageBox.Show("宽度 格式不正确，此处应该为大于零的正整数，如有小数，最大为4位小数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            this.txtWidth.Focus();
                            return;
                        }
                        if (this.txtWidth.Text.Trim().Length > 0)
                        {
                            orderItemCondition.Width = Convert.ToDouble(this.txtWidth.Text.Trim());
                        }
                    }
                    if (this.numWidthUp.Value > 0m)
                    {
                        orderItemCondition.WidthUpWarp = (int)this.numWidthUp.Value;
                    }
                    if (this.numWidthDown.Value > 0m)
                    {
                        orderItemCondition.WidthDownWarp = (int)this.numWidthDown.Value;
                    }
                    if (this.txtHeight.Text.Trim().Length > 0)
                    {
                        if (!Regex.IsMatch(this.txtHeight.Text.Trim(), "^(([0-9]{1,8})|([0-9]{1,8}\\.[0-9]{1,4}))$") || Convert.ToDouble(this.txtHeight.Text.Trim()) <= 0.0)
                        {
                            MessageBox.Show("高度 格式不正确，此处应该为大于零的正整数，如有小数，最大为4位小数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            this.txtHeight.Focus();
                            return;
                        }
                        if (this.txtHeight.Text.Trim().Length > 0)
                        {
                            orderItemCondition.Height = Convert.ToDouble(this.txtHeight.Text.Trim());
                        }
                    }
                    if (this.numHeightUp.Value > 0m)
                    {
                        orderItemCondition.HeightUpWarp = (int)this.numHeightUp.Value;
                    }
                    if (this.numHeightDown.Value > 0m)
                    {
                        orderItemCondition.HeightDownWarp = (int)this.numHeightDown.Value;
                    }
                    if (this.txtRemark.Text.Trim().Length > 128)
                    {
                        MessageBox.Show("产品类型 格式不正确，字符太长，应小于等于32个字符", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.txtRemark.Focus();
                    }
                    else
                    {
                        if (this.txtRemark.Text.Trim().Length > 0)
                        {
                            orderItemCondition.Remark = this.txtRemark.Text.Trim().Replace("'", "''").Replace(" ", "%");
                        }
                        if (this.chkSpeakForDateUsed.Checked)
                        {
                            int num = DateTime.Parse(this.dtpSpeakForDateStart.Value.ToShortDateString() + " 00:00:00").CompareTo(DateTime.Parse(this.dtpSpeakForDateEnd.Value.ToShortDateString() + " 23:59:59"));
                            if (num > 0)
                            {
                                MessageBox.Show("订购日期：后面的日期要大于或等于前面的日期", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                this.dtpSpeakForDateEnd.Focus();
                                return;
                            }
                            orderItemCondition.SpeakForDateStart = this.dtpSpeakForDateStart.Value.ToShortDateString() + " 00:00:00";
                            orderItemCondition.SpeakForDateEnd = this.dtpSpeakForDateEnd.Value.ToShortDateString() + " 23:59:59";
                        }
                        OrderItems orderItems = new OrderItems();
                        try
                        {
                            this._dtProduct = orderItems.GetOrderItemsByCondition(orderItemCondition);
                        }
                        catch (SqlException ex)
                        {
                            MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！\r\n\r\n订单项信息可能没有读出来", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        }
                        catch (Exception ex2)
                        {
                            MessageBox.Show(ex2.Message + "\r\n\r\n订单项信息可能没有读出来", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                        if (this._dtProduct != null)
                        {
                            if (this._dtProduct.Rows.Count > 0)
                            {
                                this.dgProduct.DataSource = null;
                                this.dgProduct.DataSource = this._dtProduct;
                                this.dgProduct.AutoResizeColumns();
                            }
                            else
                            {
                                MessageBox.Show("根据你的信息，在系统中没有找到你要得信息\r\n\r\n信息可能没有读出来", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                this.dgProduct.DataSource = null;
                            }
                        }
                    }
                }
            }
        }

        private void cbScope_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cbScope.SelectedIndex == 0)
            {
                this.btnSelect.Enabled = false;
                this.txtScope.Text = "";
                this.txtScope.Enabled = false;
                this.txtScope.BackColor = SystemColors.Control;
                this._nCustomerID = -1;
                this._nProjectID = -1;
            }
            if (this.cbScope.SelectedIndex == 1 || this.cbScope.SelectedIndex == 2)
            {
                this.btnSelect.Enabled = true;
                this.txtScope.Text = "";
                this.txtScope.Enabled = true;
                this.txtScope.BackColor = SystemColors.InactiveCaptionText;
                this.txtScope.ReadOnly = true;
                this.btnSelect.Focus();
                this._nCustomerID = -1;
                this._nProjectID = -1;
            }
            if (this.cbScope.SelectedIndex == 3)
            {
                this.btnSelect.Enabled = false;
                this.txtScope.Text = "";
                this.txtScope.Enabled = true;
                this.txtScope.BackColor = SystemColors.Window;
                this.txtScope.ReadOnly = false;
                this.txtScope.Focus();
                this._nCustomerID = -1;
                this._nProjectID = -1;
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (this.cbScope.SelectedIndex == 1)
            {
                CustomerData customerData = FormSelectCustomer.ShowBox();
                if (customerData != null)
                {
                    this._nCustomerID = customerData.CustomerID;
                    this.txtScope.Text = customerData.CompanyName;
                }
            }
            if (this.cbScope.SelectedIndex == 2)
            {
                ProjectData projectData = FormSelectProject.ShowBox(this._nCustomerID);
                if (projectData != null)
                {
                    this._nProjectID = projectData.ProjectID;
                    this.txtScope.Text = projectData.ProjectName;
                }
            }
        }

        private void btnProductType_Click(object sender, EventArgs e)
        {
            ProductTypeData productTypeData = FormSelectProductType.ShowBox();
            if (productTypeData != null)
            {
                this._nProductTypeID = productTypeData.ProductTypeID;
                this.txtProductType.Text = productTypeData.Name;
            }
        }

        private void chkSpeakForDateUsed_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkSpeakForDateUsed.Checked)
            {
                this.dtpSpeakForDateStart.Enabled = true;
                this.dtpSpeakForDateEnd.Enabled = true;
            }
            else
            {
                this.dtpSpeakForDateStart.Enabled = false;
                this.dtpSpeakForDateEnd.Enabled = false;
            }
        }

        private void btnShowOrderInfo_Click(object sender, EventArgs e)
        {
            try
            {
                string text = this.dgProduct.Rows[0].Cells[0].Value.ToString();
            }
            catch (Exception)
            {
                MessageBox.Show("表格中没有数据", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            int rowIndex = this.dgProduct.CurrentCell.RowIndex;
            FormOrderInfo formOrderInfo = new FormOrderInfo(this.dgProduct.Rows[rowIndex].Cells["订单号"].Value.ToString());
            formOrderInfo.MdiParent = base.MdiParent;
            formOrderInfo.TopMost = true;
            formOrderInfo.Show();
            formOrderInfo.Activate();
        }

        private void btnInvoiceList_Click(object sender, EventArgs e)
        {
            try
            {
                string text = this.dgProduct.Rows[0].Cells[0].Value.ToString();
            }
            catch
            {
                MessageBox.Show("表格中没有数据", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            int rowIndex = this.dgProduct.CurrentCell.RowIndex;
            FormInvoiceList formInvoiceList = new FormInvoiceList(this.dgProduct.Rows[rowIndex].Cells["产品ID"].Value.ToString());
            formInvoiceList.MdiParent = base.MdiParent;
            formInvoiceList.Show();
            formInvoiceList.Activate();
        }

        private void btnDetailedReports_Click(object sender, EventArgs e)
        {
            if (this._dtProduct != null)
            {
                if (this._dtProduct.Rows.Count < 1)
                {
                    MessageBox.Show("没有数据来生成报表！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    this.saveFileDialog1.Filter = "Excel文件|*.xls";
                    this.saveFileDialog1.Title = "请选择你要保存的路径和文件名";
                    this.saveFileDialog1.FileName = "订单总结_Report.xls";
                    string text = null;
                    if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        text = this.saveFileDialog1.FileName;
                        this.saveFileDialog1.Dispose();
                        try
                        {
                            File.Copy(Application.StartupPath + "\\model\\" + AppSettings.GetExcelSettings("detailedReports"), text, true);
                        }
                        catch (IOException ex)
                        {
                            MessageBox.Show(ex.Message + "\r\n或者是: 和你要创建的相同文件名的目标文件正在使用，不能覆盖，请把它关闭或删除后在操作", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            return;
                        }
                        catch (Exception ex2)
                        {
                            MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            return;
                        }
                        try
                        {
                            ExcelOperationOrderDetailedReports excelOperationOrderDetailedReports = new ExcelOperationOrderDetailedReports(Application.StartupPath);
                            excelOperationOrderDetailedReports.MakeDataToExcel(text, this._dtProduct);
                        }
                        catch (Exception ex2)
                        {
                            MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                    else
                    {
                        this.saveFileDialog1.Dispose();
                    }
                }
            }
            else
            {
                MessageBox.Show("没有数据来生成报表！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void btnDetailedReports2_Click(object sender, EventArgs e)
        {
            if (this._dtProduct != null)
            {
                if (this._dtProduct.Rows.Count < 1)
                {
                    MessageBox.Show("没有数据来生成报表！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    this.saveFileDialog1.Filter = "Excel文件|*.xls";
                    this.saveFileDialog1.Title = "请选择你要保存的路径和文件名";
                    this.saveFileDialog1.InitialDirectory = AppSettings.GetAppSettings("exportOrderPath");
                    this.saveFileDialog1.FileName = "订单明细.xls";
                    if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        string fileName = this.saveFileDialog1.FileName;
                        General general = null;
                        try
                        {
                            general = new General(Application.StartupPath + "\\model\\" + AppSettings.GetExcelSettings("youhua"), true);
                            general.MakeDataToExcel(fileName, this._dtProduct);
                        }
                        catch (IOException ex)
                        {
                            MessageBox.Show(ex.Message + "\r\n或者是: 和你要创建的相同文件名的目标文件正在使用，不能覆盖，请把它关闭或删除后在操作", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                        catch (Exception ex2)
                        {
                            MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                        if (general != null)
                        {
                            general.Dispose();
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("没有数据来生成报表！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }
    }
}
